using System.Collections;
using UnityEngine;

public class LiquidFallingController : MonoBehaviour
{
	private LiquidFalling liquidFallingBackground;

	private LiquidFalling liquidFallingForeground;

	private MeshRenderer meshRendererliquidFalling;

	private Transform bottle;

	private Transform glass;

	private GlassFillingUpController glassFillingUpController;

	private float lastWidth;

	private Vector3 start;

	private Vector3 end;

	public Vector3 handle1 = new Vector3(0f, 2.5f);

	public Vector3 handle2 = new Vector3(-1.2f, 10.68f);

	[Range(0f, 1f)]
	public float from;

	[Range(0f, 1f)]
	public float to = 1f;

	private bool endAnimation;

	private GameController gameController;

	public bool zeroLiquidValue;

	private bool touchGlassLiquid;

	private void Awake()
	{
		liquidFallingBackground = base.transform.FindDeepChild("LiquidFallingBackground").GetComponent<LiquidFalling>();
		liquidFallingForeground = base.transform.FindDeepChild("LiquidFallingForeground").GetComponent<LiquidFalling>();
		meshRendererliquidFalling = liquidFallingBackground.GetComponent<MeshRenderer>();
		gameController = UnityEngine.Object.FindObjectOfType<GameController>();
	}

	public void SetBottleTransform(Transform trans)
	{
		bottle = trans;
	}

	private void SetNewStartPosition()
	{
		Vector3 position = glass.transform.position;
		float x = position.x;
		Vector3 position2 = glass.transform.position;
		start = new Vector3(x, position2.y);
	}

	private void SetNewEndPosition()
	{
		if (bottle != null)
		{
			end = bottle.transform.position;
		}
	}

	public void StartAnimation()
	{
		StartCoroutine(Animation());
	}

	public void StopAnimation()
	{
		endAnimation = true;
		Vector3 vector = BezierMeshCalculator.PointOnPath(from, start, handle1 + liquidFallingBackground.transform.position, handle2 + liquidFallingBackground.transform.position, end);
		if (IsPointBelowLiquidInGlass(vector.y))
		{
			touchGlassLiquid = true;
		}
	}

	private IEnumerator Animation()
	{
		float endStart = 0f;
		bool savedEndStart = false;
		float from = 1f;
		float to = 1f;
		SetNewStartPosition();
		StopSparklingSound();
		bool isFizzy = LiquidTypes.IsFizzy(glassFillingUpController.GetLastLiquid().type);
		AudioSource soundObject = PlayLiquidSound(isFizzy);
		zeroLiquidValue = false;
		while (true)
		{
			float animationSpeed = Time.deltaTime * 3f;
			if (!endAnimation)
			{
				from = Mathf.Max(0f, from - animationSpeed);
				SetNewEndPosition();
			}
			else
			{
				from = Mathf.Max(0f, from - animationSpeed);
				to = Mathf.Max(0f, to - animationSpeed);
			}
			this.from = from;
			this.to = to;
			SetAnimationFrame();
			Vector3 posFrom = BezierMeshCalculator.PointOnPath(from, start, handle1 + liquidFallingBackground.transform.position, handle2 + liquidFallingBackground.transform.position, end);
			if (IsPointBelowLiquidInGlass(posFrom.y))
			{
				if (!savedEndStart)
				{
					savedEndStart = true;
					endStart = this.from;
				}
				if (!endAnimation)
				{
					gameController.GlassFillingUp(lastWidth);
				}
				else
				{
					if (!zeroLiquidValue && touchGlassLiquid)
					{
						zeroLiquidValue = true;
						gameController.GlassFillingUp(0f);
					}
					if (!zeroLiquidValue && !touchGlassLiquid)
					{
						zeroLiquidValue = true;
						ConstGlassFillingUp.CreateObject(gameController);
					}
				}
				Vector3 vector = BezierMeshCalculator.PointOnPath(to, start, handle1 + liquidFallingBackground.transform.position, handle2 + liquidFallingBackground.transform.position, end);
				if (IsPointBelowLiquidInGlass(vector.y))
				{
					break;
				}
			}
			if (endStart >= to)
			{
				break;
			}
			yield return new WaitForEndOfFrame();
		}
		if (soundObject != null && soundObject.isPlaying)
		{
			soundObject.Stop();
		}
		if (isFizzy && gameController.FillingUpEnabled)
		{
			AudioController.instance.Play("sparkling_water_in_glass_loop", null, multipleScenes: false, loop: true);
		}
		if (!zeroLiquidValue)
		{
			gameController.GlassFillingUp(0f);
		}
		UnityEngine.Object.Destroy(base.gameObject);
	}

	private bool IsPointBelowLiquidInGlass(float pointPosY)
	{
		return Mathf.Approximately(pointPosY, glassFillingUpController.GetLastLiquidUpperPosition()) || pointPosY <= glassFillingUpController.GetLastLiquidUpperPosition();
	}

	private static void StopSparklingSound()
	{
		GameObject gameObject = AudioController.instance.Exist("sparkling_water_in_glass_loop");
		if (gameObject != null)
		{
			AudioSource component = gameObject.GetComponent<AudioSource>();
			if (component.isPlaying)
			{
				gameObject.GetComponent<AudioSource>().Stop();
			}
		}
	}

	private AudioSource PlayLiquidSound(bool isFizzy)
	{
		string empty = string.Empty;
		empty = (glassFillingUpController.IsGlassEmpty() ? ((!isFizzy) ? "pour_still_water_empty" : "pour_sparkling_water_empty") : ((!isFizzy) ? "pour_still_water_half" : "pour_sparkling_water_half"));
		GameObject gameObject = AudioController.instance.Play(empty);
		if (gameObject != null)
		{
			return gameObject.GetComponent<AudioSource>();
		}
		return null;
	}

	public void SetWidth(float width)
	{
		lastWidth = width;
		liquidFallingBackground.SetWidth(width);
		liquidFallingForeground.SetWidth(width * 0.5f);
	}

	public void SetAnimationFrame()
	{
		liquidFallingBackground.SetPosition(start, end, handle1, handle2, from, to);
		liquidFallingForeground.SetPosition(start, end, handle1, handle2, from, to);
		Transform transform = liquidFallingBackground.transform;
		Vector3 position = liquidFallingBackground.transform.position;
		float x = position.x;
		Vector3 position2 = liquidFallingBackground.transform.position;
		transform.position = new Vector3(x, position2.y, 4.8f);
		Transform transform2 = liquidFallingForeground.transform;
		Vector3 position3 = liquidFallingForeground.transform.position;
		float x2 = position3.x;
		Vector3 position4 = liquidFallingForeground.transform.position;
		transform2.position = new Vector3(x2, position4.y, 4.7f);
	}

	public void SetMaterials(Material backgroundFluidMaterial, Material foregroundFluidMaterial)
	{
		liquidFallingBackground.SetMaterial(backgroundFluidMaterial);
		liquidFallingForeground.SetMaterial(foregroundFluidMaterial);
	}

	public void SetGlassTransform(Transform glassFront)
	{
		glass = glassFront;
		glassFillingUpController = glassFront.GetComponent<GlassFillingUpController>();
	}

	private void OnDrawGizmos()
	{
		Gizmos.color = Color.red;
		Gizmos.DrawSphere(base.transform.TransformPoint(start), 0.1f);
		Gizmos.color = Color.green;
		Gizmos.DrawSphere(base.transform.TransformPoint(end), 0.1f);
	}
}
